import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import Fenglei from '../views/Fenglei.vue'
import SearchSection from '../views/SearchSection.vue'
import MineView from '../views/MineView.vue'
import ShoppingCar from '@/components/ShoppingCar.vue'
import OrderConfirmation from '@/components/OrderConfirmation.vue'
import PendingPayment from '@/components/PendingPayment.vue'
import PendingShipment from '@/components/PendingShipment.vue'
import PendingReceipt from '@/components/PendingReceipt.vue'
import PendingEvaluated from '@/components/PendingEvaluated.vue'
import FarGot from '@/components/FarGot.vue'
import ModifyPsw from '@/components/ModifyPsw.vue'
import Popular from '@/views/Popular.vue'
import LoginViewVue from '@/views/LoginView.vue'
import RegisterPage from '@/views/RegisterPage.vue';
import OutFit from '@/views/OutFit.vue'
import BeaUty from '@/views/BeaUtyView.vue'
import TiaoZhuan from '@/views/TiaoZhuan.vue'
import SearchResult from '@/views/SearchResult.vue'
import LivePlayback from '@/views/LivePlayback.vue'
import GuanZhu from '@/views/GuanZhu.vue'
import SanView from '@/views/SanView.vue'
import LiveDetail from '@/views/LiveDetail.vue'
import ShouChang from '@/views/ShouChang.vue'
import { useUserStore } from '@/stores/user';
import commonRateList from '@/views/commonRateList.vue'
import liaotian from '@/views/liaotian.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
         path: '/liaotian',
         name: 'liaotian',
         component: liaotian,
        },
  
      {
        path: '/',
        name: 'home',
        component: HomeView,
        meta:
        {
          showNav: true, dibudaoh: true,
        }

      },
      {
        path: '/common-rate-list/:iid',
        name: 'common-rate-list',
        component: commonRateList,
      },
      {
        path: '/about',
        name: 'about',
        component: AboutView,

      },
      {
        path: '/fenglei',
        name: 'fenglei',
        component: Fenglei,
      },
      {
        path: '/shop',
        name: 'shop',
        component: SearchSection,
      },
      {
        path: '/Mine',
        name: 'Mine',
        component: MineView,
        meta: {
          needLogin: true, dibudaoh: true,
        },
      },
      {
        path: '/shoppingcar',
        name: 'shoppingcar',
        component: ShoppingCar,
      },
      {
        path: '/orderCon',
        name: 'orderCon',
        component: OrderConfirmation,
      },
      {
        path: '/Pending',
        name: 'Pending',
        component: PendingPayment,
      },
      {
        path: '/shipment',
        name: 'shipment',
        component: PendingShipment,
      },
      {
        path: '/receipt',
        name: 'receipt',
        component: PendingReceipt,
      },
      {
        path: '/evaluated',
        name: 'evaluated',
        component: PendingEvaluated,
      },
      {
        path: '/popular',
        name: 'popular',
        component: Popular,
        meta:
        {
          dibudaoh: true,
        }
      },
      {
        path: '/login',
        name: 'login',
        component: LoginViewVue,
      },
      {
        path: '/register',
        name: 'register',
        component: RegisterPage
      },
      {
        path: '/forgot',
        name: 'forgot',
        component: FarGot,
      },
      {
        path: '/modify',
        name: 'modify',
        component: ModifyPsw,
      },

      {
        path: '/outfit',
        name: 'outfit',
        component: OutFit,
        meta:
        {
          dibudaoh: true,
        }
      },
      {
        path: '/beauty',
        name: 'beauty',
        component: BeaUty,
        meta:
        {
          dibudaoh: true,
        }
      },
      {
        path: '/TiaoZhuan/:iid',
        name: 'TiaoZhuan',
        component: TiaoZhuan,
        meta: {
          dibudaoh: false,
        }

      },
      {
        path: '/SearchResult',
        name: 'SearchResult',
        component: SearchResult
      },
      {
        path: '/LivePlayback',
        name: 'LivePlayback',
        component: LivePlayback
      },
      {
        path: '/GuanZhu',
        name: 'GuanZhu',
        component: GuanZhu,
        meta:
        {
          showNav: true, dibudaoh: true,
        }
      },
      {
        path: '/sanview',
        name: 'sanview',
        component: SanView,
        meta:
        {
          dibudaoh: true,
        },
      },
      {
        path: '/ShouChang',
        name: 'ShouChang',
        component: ShouChang,
        meta:
        {
          showNav: false, dibudaoh: false,
        }
      },

      { path: '/live/:roomId/:actorId', name: 'LiveDetail', component: LiveDetail },
    ]
  })
  //登录跳转
  router.beforeEach((to, from, next) => {
    const userStore = useUserStore();
    userStore.initToken(); // 从本地存储初始化 token
    let isLogin = Boolean(userStore.token);
    console.log('xxxxxxxxxxxxx', userStore.token);

    if (to.meta.needLogin && !isLogin) {
      console.log("需要登录", "目标页面是", to.fullPath);
      next({
        path: "/login",
        query: {
          target:
            to.fullPath,
        }
      });
    } else {
      next();
    }
  });

  export default router
